/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package telebim;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JPanel;
import Logika.*;
import java.awt.geom.AffineTransform;

/**
 *
 * @author mejcu
 */
public class Plansza extends JPanel {

    private int PngWidth = 44;
    private int PngHight = 44;
    private BufferedImage[] czolgi = new BufferedImage[4];
    private BufferedImage zdrowie;
    private int[] values = new int[3];
    private int[][] Layout;
    private TankMover[] tankMover;
    private BufferedImage[] pictures = new BufferedImage[10];


    {
        setOpaque(false);
    }
    private int[] tankInfo = new int[4];

    /**
     *
     * @param tab
     */
    public void setTankInfo(int[] tab) {
        tankInfo = tab;
    }

    /**
     *
     */
    public Plansza() {
        tankMover = new TankMover[4];
        for (int i = 0; i < tankMover.length; i++) {
            tankMover[i] = new TankMover();

        }
        try {
            for (int i = 0; i < 10; i++) {
                if (i != 3) {
                    pictures[i] = ImageIO.read(new File("src/Pic/" + i + ".jpg"));
                } else {
                    pictures[i] = ImageIO.read(new File("src/Pic/3.png"));
                }

            }
            zdrowie = ImageIO.read(new File("src/Pic/Life.jpg"));
            for (int i = 0; i < czolgi.length; i++) {
                czolgi[i] = ImageIO.read(new File("src/Pic/8_" + (i + 1) + ".jpg"));

            }
        } catch (IOException e) {
            System.err.println("Blad podczas wczytywania grafiki");
            System.exit(1);
        }
        Thread mover = new Thread() {

            @Override
            public void run() {
                while (true) {
                    for (int i = 0; i < 4; i++) {

                        if (tankInfo[i] == 1) {
                            if (tankMover[i].dy < 44 && tankMover[i].dy > -44) {
                                tankMover[i].dy += tankMover[i].kJ - tankMover[i].sJ;
                            }
                            if (tankMover[i].dx < 44 && tankMover[i].dx > -44) {
                                tankMover[i].dx += tankMover[i].kI - tankMover[i].sI;
                            }
                        }
                    }
                    try {
                        repaint();
                        sleep(10);
                    } catch (InterruptedException ex) {
                        System.out.println("server sleep");
                    }
                }
            }
        };
        mover.start();
    }
    int pociskDX = 0, pociskDY = 0;

    /**
     *
     * @param data
     */
    public void getLayout(String data) {
        int k = 0;
        for (int i = 0; i < 12; i++) {
            for (int j = 0; j < 16; j++) {
                Layout[i][j] = data.charAt(k++);
            }
        }
    }

    /**
     *
     * @param tab
     */
    public void setMap(int[][] tab) {
        Layout = tab;
    }

    private int[] getObjectDescription(int value) {
        values[0] = value / 100;//postac
        int temp_value = value / 10;
        values[1] = temp_value % 10;//kierunek
        values[2] = value % 10;//co
        return values;

    }

    @Override
    public void paintComponent(Graphics g) {

        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        AffineTransform affineTransform;
        int postac, kierunek;
        {
            for (int i = 0; i < Zmienne.WYSOKOSC; i++) {
                for (int j = 0; j < Zmienne.SZEROKOSC; j++) {
                    postac = Layout[j][i] % 10; // modulo just to get the type of object
                    kierunek = Layout[j][i] / 10; //division to get the direction
                    if (Layout[j][i] > 100) {
                        values = getObjectDescription(Layout[j][i]);
                        postac = values[2];
                    } else {
                        postac = Layout[j][i];
                    }
                    switch (postac) {
                        case Zmienne.TANK:
                        case Zmienne.TANK_IN_WOOD:

                            if (tankMover[values[0] - 1].sI == 0 && tankMover[values[0] - 1].sJ == 0 &&
                                tankMover[values[0] - 1].dx == 0 && tankMover[values[0] - 1].dy == 0) {
                                tankMover[values[0] - 1].sI = i;
                                tankMover[values[0] - 1].sJ = j;
                                tankMover[values[0] - 1].kI = i;
                                tankMover[values[0] - 1].kJ = j;
                            }

                            g.drawImage(pictures[0], i * 44 + (this.getSize().width - 44 * 12) / 2, j * 44 +
                                        (this.getSize().height - 44 * 16) / 2, null);
                            break;
                        case Zmienne.WOOD:

                            g.drawImage(pictures[0], i * 44 + (this.getSize().width - 44 * 12) / 2, j * 44 +
                                        (this.getSize().height - 44 * 16) / 2, null);
                            break;
                        case Zmienne.BONUS_LIFE:
                        case Zmienne.BONUS_LIFE + Zmienne.WOOD:

                            g.drawImage(zdrowie, i * 44 + (this.getSize().width - 44 * 12) / 2, j * 44 +
                                        (this.getSize().height - 44 * 16) / 2, null);
                            break;
                        default:
                            if (postac > 18) {
                                affineTransform = new AffineTransform();
                                affineTransform.rotate(Math.toRadians((postac / 10 - 1) * 90), i * 44 +
                                                        22 + (this.getSize().width - 44 * 12) / 2, j * 44 + 22 +
                                                        (this.getSize().height - 44 * 16) / 2);
                                affineTransform.translate(i * 44 + (this.getSize().width - 44 * 12) / 2, j * 44 +
                                                          (this.getSize().height - 44 * 16) / 2);
                                g2d.drawImage(pictures[9], affineTransform, this);
                            } 
                            else
                            {
                                if (Layout[j][i] % 10 != Zmienne.TANK_IN_WOOD && Layout[j][i] % 10 != Zmienne.WOOD) {
                                    g.drawImage(pictures[Layout[j][i] % 10], i * 44 + (this.getSize().width - 44 * 12) / 2,
                                                j * 44 + (this.getSize().height - 44 * 16) / 2, null);
                                }
                            }
                            break;
                    }
                }
            }
        }


        for (int i = 0; i < Zmienne.WYSOKOSC; i++) {
            for (int j = 0; j < Zmienne.SZEROKOSC; j++) {
                postac = Layout[j][i] % 10; // modulo just to get the type of object
                switch (postac) {
                    case Zmienne.TANK:
                    case Zmienne.TANK_IN_WOOD:
                        kierunek = getObjectDescription(Layout[j][i])[1];
                        affineTransform = new AffineTransform();
                        affineTransform.rotate(Math.toRadians((kierunek - 1) * 90), tankMover[values[0] - 1].sI * 44 +
                                               (this.getSize().width - 44 * 12) / 2 + tankMover[values[0] - 1].dx + 22,
                                               tankMover[values[0] - 1].sJ * 44 + (this.getSize().height - 44 * 16) / 2+
                                               tankMover[values[0] - 1].dy + 22);
                        affineTransform.translate(tankMover[values[0] - 1].sI * 44 + (this.getSize().width - 44 * 12) / 2+
                                                    tankMover[values[0] - 1].dx, tankMover[values[0] - 1].sJ * 44 +
                                                    (this.getSize().height - 44 * 16) / 2 + tankMover[values[0] - 1].dy);
                        g2d.drawImage(czolgi[values[0] - 1], affineTransform, this);
                        break;
                }
            }
        }
        for (int i = 0; i < Zmienne.WYSOKOSC; i++) {
            for (int j = 0; j < Zmienne.SZEROKOSC; j++) {
                postac = Layout[j][i] % 10; // modulo just to get the type of object
                switch (postac) {
                    case Zmienne.WOOD:
                    case Zmienne.TANK_IN_WOOD:
                        g.drawImage(pictures[3], i * 44 + (this.getSize().width - 44 * 12) / 2, j * 44 +
                                    (this.getSize().height - 44 * 16) / 2, null);
                        break;
                }
            }
        }
    }

    /**
     *
     * @return
     */
    public Dimension getPrefferedSize() {
        return new Dimension(12 * 44, 16 * 44);
    }

    /**
     * 
     * @param _kJ
     * @param _kI
     * @param _sJ
     * @param _sI
     */
    public void moveTank(int _kJ, int _kI, int _sJ, int _sI) {
        int i = (int) (Layout[_kJ][_kI] / 100) - 1;

        tankMover[i].sI = _sI;
        tankMover[i].sJ = _sJ;
        tankMover[i].kI = _kI;
        tankMover[i].kJ = _kJ;
        tankMover[i].dx = 0;
        tankMover[i].dy = 0;
    }

    /**
     *
     */
    public class TankMover {

        int sI, sJ, kI, kJ;
        int dx, dy;

        /**
         *
         */
        public TankMover() {
            sI = 0;
            sJ = 0;
            kI = 0;
            kJ = 0;
            dx = 0;
            dy = 0;
        }
    }

    /**
     *
     * @param i
     * @param j
     * @param change
     */
    public void changeLayout(int i, int j, int change) {
        Layout[i][j] = change;
    }
}
